﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using System.ComponentModel;

namespace WpfApplication
{
    enum Mode
    {
        ConstantVoltage,
        ConstantCurrent
    }

    class ACPSController : IPowerSignal
    {
        //private double voltage;
        //private double current;
        private Mode operatingMode;

        public ACPSController() {
        }

        #region Properties
        public double Voltage { get; set; }
        public double Current { get; set; }
        //public double Voltage
        //{
        //    get { return voltage; }
        //    set
        //    {
        //        voltage = value;
        //    }
        //}
        //public double Current
        //{
        //    get { return current; }
        //    set 
        //    { 
        //        current = value;
        //    }
        //}
        public Mode OperatingMode
        {
            get { return this.operatingMode; }
            set{ this.operatingMode = value; }
        }

        #endregion Properties

        #region Public Methods
        public void SetVoltage(double level)
        {
            //if ((level >= 0.0) && (level <= voltageMax))
            //{
                this.Voltage = level;
                SetCurrent(this.Current);
            //}
            //else
            //    throw new ArgumentOutOfRangeException();
        }

        public void SetCurrent(double level)
        {
            //if ((level < 0.0) || (level > SignalConstants.currentMax)) throw new ArgumentOutOfRangeException();
            //Contract.EndContractBlock();
            this.Current = level;
            double currentHigh = SetCurrentAllowed();
            if (level > currentHigh)
                this.Current = currentHigh;
        }
 
        public double SetCurrentAllowed()
        {
            double adjustedCurrent = (-0.05) * this.Voltage + 13.25;
            if (this.Voltage >= SignalConstants.voltcurrMax)
                return adjustedCurrent;
            else
                return SignalConstants.currentMax;
        }

        public Mode ReadMode(Load impedance)
        {
            if (impedance.Resistance >= (this.Voltage / this.Current))
                this.OperatingMode = Mode.ConstantVoltage;
            else
                this.OperatingMode = Mode.ConstantCurrent;
            return OperatingMode;
        }
        #endregion
    }
}
